//
//  ThemeChooserView.m
//  LabanBrowser
//
//  Created by hoangtx on 4/2/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "ThemeChooserView.h"
#import "StaticVariables.h"
#import "MyUtilities.h"
@implementation ThemeChooserView{
    CGRect onRect;
    CGRect offRect;
    UIToolbar* pickerToolbar;
    UILabel* titleLB;
}
@synthesize picker;

@synthesize themeList;
@synthesize delegate;

-(void)initialize{
    // Initialization code
    themeList=[[NSMutableArray alloc]init];
    [self reloadThemeData];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    int screenWidth=screenSize.width;
    int screenHeight=screenSize.height;
        
    
    
    CGRect pickerFrame = CGRectMake(0, pickerToolbar.frame.size.height, 0, 0);
    picker = [[UIPickerView alloc] initWithFrame:pickerFrame];
    picker.showsSelectionIndicator = YES;
    picker.dataSource=self;
    picker.delegate=self;
    
    [self addSubview:picker];
    
    
    onRect=CGRectMake(0, (screenHeight-picker.frame.size.height-pickerToolbar.frame.size.height)/2, screenWidth, picker.frame.size.height+pickerToolbar.frame.size.height);
    offRect=CGRectMake(0, screenHeight, screenWidth, picker.frame.size.height+pickerToolbar.frame.size.height);
    self.frame=onRect;
    self.alpha=0;
    
    
    NSNumber* currentUserTheme=[[[StaticVariables shareVariables]userDefaults]theme];
    if(currentUserTheme==nil){
        [self.picker selectRow:0 inComponent:0 animated:YES];
        [[[StaticVariables shareVariables]userDefaults]setTheme:[NSNumber numberWithInt:0]];
    }else{
        [self.picker selectRow:[currentUserTheme intValue] inComponent:0 animated:YES];
    }
    
    
    [self addSubview:pickerToolbar];
    self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    
    //[sheet addSubview:picker];
    
    //[sheet showInView:self.view];
    //[sheet setBounds:CGRectMake(0, 0, 320, 425+pickerFrame.origin.y)];
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

-(id)init{
    self = [super init];
    if (self) {
        [self initialize];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
-(void)needToShowUp:(BOOL)_need{
    //CGRect rectToAnimateTo;
    double alphaToAnimateTo;
    //CGAffineTransform transformToAnimateTo;
    if(_need){
        //rectToAnimateTo=onRect;
        alphaToAnimateTo = 1;
        //transformToAnimateTo = onTransform;
    }else{
        //rectToAnimateTo=offRect;
        alphaToAnimateTo = 0;
        //transformToAnimateTo = offTransform;
    }
    [UIView animateWithDuration:0.25f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        //self.frame=rectToAnimateTo;
        self.alpha = alphaToAnimateTo;
        //self.transform = transformToAnimateTo;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)reloadThemeData{
    [themeList removeAllObjects];
    [themeList addObject:[MyUtilities getLocalizeStringFromTag:@"ThemeDefault"]];
    [themeList addObject:[MyUtilities getLocalizeStringFromTag:@"ThemeBlue"]];
    [themeList addObject:[MyUtilities getLocalizeStringFromTag:@"ThemeGreen"]];
    [themeList addObject:[MyUtilities getLocalizeStringFromTag:@"ThemePurple"]];
    [themeList addObject:[MyUtilities getLocalizeStringFromTag:@"ThemeRed"]];
    [themeList addObject:[MyUtilities getLocalizeStringFromTag:@"ThemeOrange"]];
    [themeList addObject:[MyUtilities getLocalizeStringFromTag:@"ThemeYellow"]];
    [picker reloadAllComponents];
    int screenWidth=[UIScreen mainScreen].bounds.size.width;
    
    [pickerToolbar removeFromSuperview];
    pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44)];
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];
    
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    
    
    NSString* themeTitle=[MyUtilities getLocalizeStringFromTag:@"ThemeTitle"];
    titleLB = [[UILabel alloc] initWithFrame:CGRectMake(250, 0, screenWidth-100, 21.0f)];
    [titleLB setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
    [titleLB setBackgroundColor:[UIColor clearColor]];
    [titleLB setTextColor:[UIColor whiteColor]];
    [titleLB setText:themeTitle];
    [titleLB setTextAlignment:NSTextAlignmentCenter];
    
    UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:titleLB];
    [barItems addObject:title];
    
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDone)];
    [barItems addObject:doneBtn];
    
    
    [pickerToolbar setItems:barItems animated:YES];
    [self addSubview:pickerToolbar];
}

-(void)pickerDone{
    [self needToShowUp:NO];
}

#pragma mark datasource

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return themeList.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    //[self needToShowUp:NO];
    [[[StaticVariables shareVariables]userDefaults]setTheme:[NSNumber numberWithInt:row]];
    if(delegate && [delegate respondsToSelector:@selector(onFinishChoosingTheme)]){
        [delegate onFinishChoosingTheme];
    }
        
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [themeList objectAtIndex:row];
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 50;
}

@end
